package com.eDriveV2;


import java.io.File;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;

import android.os.Environment;
import android.util.Log;

import javax.xml.parsers.SAXParserFactory;

/**
 * Here is sample of reading attributes of a given XML element.
 */

public class XMLReader {
	private static ArrayList<Integer> time = new ArrayList();
	private static ArrayList<String> location = new ArrayList();
	private static ArrayList<Integer> speed = new ArrayList();
	private static ArrayList<Double> efficiency = new ArrayList();
	private static ArrayList<String> efficiencyLabel = new ArrayList();
    /**
     * Application entry point
     * @param args command-line arguments
     */
    public XMLReader(String filename) {
    	
        try {
        	
            // creates and returns new instance of SAX-implementation:
            SAXParserFactory factory = SAXParserFactory.newInstance();
            
            // create SAX-parser...
            SAXParser parser = factory.newSAXParser();
            org.xml.sax.XMLReader xr = parser.getXMLReader();
            // .. define our handler:
            SaxHandler handler = new SaxHandler();
            
            URL sourceUrl = new URL("file://"+Environment.getExternalStorageDirectory()+"/eDriveData/rides/"+filename);
            // and parse:
            xr.setContentHandler(handler);
            xr.parse(new InputSource(sourceUrl.openStream()));

        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        }
    }
    public ArrayList[] readXML()
    {
    	ArrayList[] list = new ArrayList[5];
    	list[0]= time;
    	list[1]= location;
    	list[2]= speed;
    	list[3]= efficiency;
    	list[4]= efficiencyLabel;
    	return list;
    	
    }
    
    /**
     * Our own implementation of SAX handler reading
     * a purchase-order data.
     */
    private static final class SaxHandler extends DefaultHandler {
    	
        // we enter to element 'qName':
        public void startElement(String uri, String localName,
                String qName, Attributes attrs) throws SAXException {
            Log.d("SCOTT", "Begin SAX");
            if (qName.equals("Wpt")) {
                // order date value as String:
                String latitudeValue = attrs.getValue("lat");
                // order number as a String:
                String longitudeValue = attrs.getValue("long");
                int speedValue = Integer.parseInt(attrs.getValue("speed"));
                double efficiencyValue = Double.parseDouble(attrs.getValue("eff"));
                String effLabel = attrs.getValue("effL");
                int timeValue = Integer.parseInt(attrs.getValue("t"));
                String locationValue = latitudeValue+","+longitudeValue;
                efficiency.add(efficiencyValue);
                speed.add(speedValue);
                time.add(timeValue);    
                location.add(locationValue);
                efficiencyLabel.add(effLabel);
             }
            Log.d("SCOTT", "END SAX");
        }
    }
}